package tool

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/xormplus/xorm"
)

type Orm struct {
	*xorm.Engine
}

var DbEngine *Orm

//var _cfg *Config
func OrmEngine(cfg *Config) (*Orm, error) {
	database := cfg.Database
	conn := database.User + ":" + database.Password + "@tcp(" + database.Host + ":" + database.Port + ")/" + database.DbName + "?chartset" + database.Charset
	engine, err := xorm.NewEngine(database.Driver, conn)
	if err != nil {
		fmt.Println("---------------数据库连接失败------------------")
		return nil, err
	}
	engine.ShowSQL(database.ShowSql)
	//if errSms := engine.Sync2(new(model.SmsCode)); errSms != nil{
	//	fmt.Println("-------------数据库映射失败-------------")
	//	return nil,errSms
	//}
	orm := new(Orm)
	orm.Engine = engine
	DbEngine = orm
	return orm, nil
}
